package org.example.dao;

import org.example.pojo.Order;
import org.example.util.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class OrderDao {


    public List<Order> getOrdersByProductId(int productId) {
        List<Order> orders = new ArrayList<>();
        String query = "SELECT o.id, o.user_id, o.product_id, o.quantity, p.name AS product_name, p.price AS unit_price " +
                "FROM orders o " +
                "JOIN products p ON o.product_id = p.id " +
                "WHERE o.product_id = ?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement ps = conn.prepareStatement(query)) {
            ps.setInt(1, productId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                orders.add(new Order(
                        rs.getInt("id"),
                        rs.getInt("user_id"),
                        rs.getInt("product_id"),
                        rs.getInt("quantity"),
                        rs.getString("product_name"),
                        rs.getDouble("unit_price")
                ));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orders;
    }



    // 根据用户 ID 获取订单列表
    public List<Order> getOrdersByUserId(int userId) {
        List<Order> orders = new ArrayList<>();
        String query = "SELECT o.id, o.product_id, o.quantity, p.name AS product_name, p.price AS unit_price " +
                "FROM orders o " +
                "JOIN products p ON o.product_id = p.id " +
                "WHERE o.user_id = ?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement ps = conn.prepareStatement(query)) {
            ps.setInt(1, userId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                orders.add(new Order(
                        rs.getInt("id"),
                        userId,
                        rs.getInt("product_id"),
                        rs.getInt("quantity"),
                        rs.getString("product_name"),
                        rs.getDouble("unit_price")
                ));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orders;
    }

    // 插入订单
    public boolean insertOrder(int userId, int productId, int quantity) {
        String query = "INSERT INTO orders (user_id, product_id, quantity) VALUES (?, ?, ?)";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement ps = conn.prepareStatement(query)) {
            ps.setInt(1, userId);
            ps.setInt(2, productId);
            ps.setInt(3, quantity);
            return ps.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 根据订单 ID 删除订单
    public boolean deleteOrderById(int orderId) {
        String query = "DELETE FROM orders WHERE id = ?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement ps = conn.prepareStatement(query)) {
            ps.setInt(1, orderId);
            return ps.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 根据订单 ID 获取订单详情
    public Order getOrderById(int orderId) {
        String query = "SELECT o.id, o.user_id, o.product_id, o.quantity, p.name AS product_name, p.price AS unit_price " +
                "FROM orders o " +
                "JOIN products p ON o.product_id = p.id " +
                "WHERE o.id = ?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement ps = conn.prepareStatement(query)) {
            ps.setInt(1, orderId);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return new Order(
                        rs.getInt("id"),
                        rs.getInt("user_id"),
                        rs.getInt("product_id"),
                        rs.getInt("quantity"),
                        rs.getString("product_name"),
                        rs.getDouble("unit_price")
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
